package com.boot.security.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boot.security.server.hitapp.api.hitapi;
import com.boot.security.server.model.AppAccount;
import com.boot.security.server.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.AppApponitDao;
import com.boot.security.server.model.AppApponit;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/appApponits")
public class AppApponitController {

    @Autowired
    private AppApponitDao appApponitDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public AppApponit save(@RequestBody AppApponit appApponit) {
        appApponitDao.save(appApponit);

        return appApponit;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public AppApponit get(@PathVariable Long id) {
        return appApponitDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public AppApponit update(@RequestBody AppApponit appApponit) {
        appApponitDao.update(appApponit);

        return appApponit;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return appApponitDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<AppApponit> list(PageTableRequest request) {
                return appApponitDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        appApponitDao.delete(id);
    }
    /**
     * 新增
     * @param appApponit
     * @return R
     */
    @PostMapping("/add")
    public R addinfo(@RequestBody AppApponit appApponit){

     //判断身份
     //插入到挂号信息表，订单表，状态为待支付。
        AppApponit u = appApponitDao.getById(appApponit.getId());
        if (u != null) {
            return new R<>(appApponitDao.update(appApponit));
        }
        appApponitDao.save(appApponit);
        String paytype="";
        String pidtype="";
        paytype=appApponit.getPaytype();
        pidtype=appApponit.getPidtype();
        //调用微信支付
        hitapi hitapis=new hitapi();
      //  return new R<>(hitapis.wxpaydd(appApponit,"gzh"));
        return new R();
    }

    /**
     * 修改
     * @param appApponit
     * @return R
     */
    @PostMapping("/upd")
    public R updinfo(@RequestBody AppApponit appApponit){

        return new R<>(appApponitDao.update(appApponit));
    }

    /**
     * 删除
     * @param appApponit
     * @return R
     */
    @PostMapping("/del")
    public R delinfo(@RequestBody AppApponit appApponit){


        return new R<>( appApponitDao.delete(appApponit.getId()));
    }

    /**
     * 查询
     * @param appApponit
     * @return R
     */
    @PostMapping("/find")
    public R findinfo(@RequestBody AppApponit appApponit){

        Map<String,Object> map = new HashMap<String,Object  >();
        //    map=entityToMap(appAccount);
        // 支付中 100   200 支付失败  0支付成功
        return new R<>(  appApponitDao.getBylist(""));

        //    return new R<>(  bxbFbqksDao.getById(bxbFbqks.getId()));
    }

    /**
     * 查询
     * @param appApponit
     * @return R
     */
    @PostMapping("/findpay")
    public R findpay(@RequestBody AppApponit appApponit){

        Map<String,Object> map = new HashMap<String,Object  >();
        //    map=entityToMap(appAccount);
        // 支付中 100   200 支付失败  0支付成功
        return new R<>(  appApponitDao.getById(appApponit.getId()));

        //    return new R<>(  bxbFbqksDao.getById(bxbFbqks.getId()));
    }

    /**
     * 退号退款
     *
     */

}
